<template>
  <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
    <lay-card>
      <lay-row>
        <lay-col :md="24" class="toolbar">
          <lay-col :md="6">
            <lay-button type="primary" size="sm" @click="add" v-permission="PerKey.DocReceived_AddOrEditView"
              prefix-icon="layui-icon-addition">新增
            </lay-button>
            <lay-button size="sm" type="normal" @click="exportExcel()" prefix-icon="layui-icon-export">导出
            </lay-button>
          </lay-col>
          <lay-col :md="18">
            <lay-form :model="queryModel" ref="layFormRef">
              <lay-row>
                <lay-col :md="3">
                  <lay-input size="sm" v-model="queryModel.title" style="width: 90%" placeholder="标题"></lay-input>
                </lay-col>
                <lay-col :md="3">
                  <lay-input size="sm" v-model="queryModel.docNumber" style="width: 90%" placeholder="文号查询"></lay-input>
                </lay-col>
                <lay-col :md="3">
                  <lay-input size="sm" v-model="queryModel.department" style="width: 90%"
                    placeholder="来文部门"></lay-input>
                </lay-col>
                <lay-col :md="2">
                  <lay-input size="sm" v-model="queryModel.clercator" style="width: 90%" placeholder="经办人"></lay-input>
                </lay-col>
                <lay-col :md="4">
                  <lay-button size="sm" type="primary" @click="GetList(queryModel)"
                    prefix-icon="layui-icon-search">查询</lay-button>
                  <lay-button @click="reset" size="sm">重置</lay-button>
                </lay-col>
              </lay-row>
            </lay-form>
          </lay-col>
        </lay-col>

        <lay-col :md="24">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" v-model:selectedKeys="selectedKeys"
            @change="change" :size="tablesize">
            <template v-slot:miji="{ data }">
              {{ dictFind(data.miji) }}
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)"
                  v-permission="PerKey.DocReceived_AddOrEditView" prefix-icon="layui-icon-edit">编辑
                </lay-button>
                <lay-button type="danger" size="sm" @click="del(data)" v-permission="PerKey.DocReceived_Delete"
                  prefix-icon="layui-icon-delete">删除</lay-button>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-col>
      </lay-row>
    </lay-card>
    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['1100px']">
      <DocReceivedPost :DocId="DocId" v-model:dialogIsOk="dialogIsOk"></DocReceivedPost>
    </lay-layer>

  </lay-container>
</template>
<script lang="ts" setup>
import { reactive, ref, toRefs, watch } from "vue";
import { layer } from "@layui/layer-vue";
import { getList, delOne } from "../../../../api/module/oas/docreceived";
import { PerKey } from "../../../../library/permissionConst"
import { useRoute } from "vue-router";
import Util from '../../../../library/Util'
import { DocReceived } from "../../../../types/doc";
import DocReceivedPost from './components/DocReceivedPost.vue'

const selectedKeys = ref(["1"]);
const page = ref({ total: 0, limit: 10, current: 1, showCount: true, showRefresh: true, limits: [10, 15, 20, 50, 100, 200] });
const tablesize = ref('sm');
const columns = [
  { title: "Id", width: "50px", key: "id", align: "center", },
  { title: "标题", key: "title", },
  { title: "文号", width: "200px", key: "docNumber", align: "center", },
  { title: "密级", customSlot: "miji", width: "80px", align: "center" },
  { title: "经办人", width: "100px", key: "clercator", align: "center", },
  { title: "来文时间", width: "140px", key: "greatTime", align: "center", },
  { title: "来文部门", width: "140px", key: "department", align: "center", },
  { title: "登记时间", width: "140px", key: "insertTime", align: "center", },
  { title: "登记人", width: "80px", key: "nickname", align: "center", },
  { title: "操作", width: "180px", customSlot: "operator", key: "operator", align: "center", }
]
const dataSource = ref([])
const route = useRoute();
//定义模型
const putData = reactive({
  queryModel: {
    Cid: 0,
    pageNum: page.value.current,
    pageSize: page.value.limit,
    title: null,
    clercator: null,
    department: null,
    docNumber: null,
    exportExcel: false,
  },
  form: <DocReceived><unknown>[]
})
const layFormRef = ref();
const reset = function () {
  layFormRef.value.reset();
}
const { queryModel } = toRefs(putData)
const GetList = function (query: any) {
  if (query != null) {
    queryModel.value.title = query.title
    queryModel.value.department = query.department
    queryModel.value.clercator = query.clercator
    queryModel.value.docNumber = query.docNumber
  }
  getList(queryModel.value).then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
      page.value.total = res.data.count
    }
  })
}

GetList(queryModel.value)
let dictList = Util.dictfilter("Sys_SecretLevel")
const dictFind = (key: number) => {
  return Util.dictFind(dictList, key);
}
const exportExcel = () => {
  layer.confirm("确认导出所有列表", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        queryModel.value.exportExcel = true
        getList(queryModel.value).then((res) => {
          const { status, filePath, fileName } = res
          if (status == 1) {
            Util.downloadFileForUrl(filePath, fileName)
            layer.close(layerId);
          } else {
            layer.msg('导出失败');
          }
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

//监听参数变化
watch(() => route.query.Cid, (val: any) => {
  queryModel.value.Cid = val;
  queryModel.value.pageNum = 1;
  GetList(queryModel.value)
}, { immediate: true })

const change = function ({ current, limit }: any) {
  queryModel.value.pageNum = current
  queryModel.value.pageSize = limit
  GetList(queryModel.value);
};
//修改
const edit = function (id: number) {
  changeVisible(id)
}
//添加
const add = function () {
  changeVisible(0)
}
//删除
const del = function (data: any) {
  layer.confirm("确认删除ID: " + data.id + " " + data.name + "的人员信息吗？", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList(queryModel.value)
            });
          } else {
            layer.msg(res.msg, { time: 1000, icon: 2 });
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

//模态操作
let addoredittip = ref();
let DocId = ref()
const modelVisible = ref(false)
const dialogIsOk = ref()
const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  dialogIsOk.value = 0
  DocId.value = id
  if (id == 0) {
    addoredittip.value = "新增";
    return
  } else {
    addoredittip.value = "编辑"
    return
  }
}

watch(dialogIsOk, (val, old) => {
  if (val == 1) {
    modelVisible.value = !modelVisible.value
    GetList(queryModel.value)
  }
})


</script>
<style scoped>
.toolbar{margin-bottom: 10px;}
</style>